#include "chronograph.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLCDNumber>
#include "config.h"

namespace Timer {

Chronograph::Chronograph(QWidget *parent) : QWidget(parent) {
	// this->setWindowTitle("秒表"); // 设置窗口标题
    // this->resize(550, 400); // 设置窗口大小
	// this->setFixedSize(500, 400); // 设置窗口大小不可变

    // 时间显示控件的布局
    QHBoxLayout * time_table = new QHBoxLayout();
    time_table->setSpacing(0); // 设置控件之间的间距
    // LCDNumber控件显示数字
    hour_lcd = new QLCDNumber();
    hour_lcd->setSegmentStyle(QLCDNumber::Filled);
    hour_lcd->setMaximumSize(100, 100); // 设置最大尺寸
    hour_lcd->setDigitCount(2);
	hour_lcd->display("00");
    minute_lcd = new QLCDNumber(2);
    minute_lcd->setSegmentStyle(QLCDNumber::Filled);
    minute_lcd->setMaximumSize(100, 100);
    minute_lcd->display("00");
    second_lcd = new QLCDNumber(2);
    second_lcd->setSegmentStyle(QLCDNumber::Filled);
    second_lcd->setMaximumSize(100, 100);
    second_lcd->display("00");
	unsigned int h , m, s;
	Timer::get_chronograph_time(h, m, s);
	if(h != 0 || m != 0 || s != 0){
		hour_lcd->display(static_cast<int>(h));
		minute_lcd->display(static_cast<int>(m));
		second_lcd->display(static_cast<int>(s));
		time.set_hours(h);
		time.set_minutes(m);
		time.set_secends(s);
	}
    // ":" 分隔符
    QFont font;
    font.setPointSize(30);
    QLabel * colon1 = new QLabel(":");
    colon1->setAlignment(Qt::AlignCenter);
    colon1->setFont(font);
    QLabel * colon2 = new QLabel(":");
    colon2->setAlignment(Qt::AlignCenter);
    colon2->setFont(font);

    time_table->addWidget(hour_lcd);
    time_table->addWidget(colon1);
    time_table->addWidget(minute_lcd);
    time_table->addWidget(colon2);
    time_table->addWidget(second_lcd);

    // 按钮组建的布局
	QVBoxLayout * button_table = new QVBoxLayout();
	start_button = new QPushButton("开始");
	reset_record_button = new QPushButton("清零");
	save_button = new QPushButton("保存");
	button_table->addWidget(start_button);
	button_table->addWidget(reset_record_button);
	button_table->addWidget(save_button);
     
    // 总布局
    QVBoxLayout * main_layout = new QVBoxLayout(this);
    main_layout->addLayout(time_table);
    main_layout->addLayout(button_table);

    this->setLayout(main_layout);

    // 连接信号和槽
    this->timer = new QTimer(this);
    this->timer->setInterval(1000); // 设置定时器间隔为1秒
    this->timer->setTimerType(Qt::PreciseTimer); // 设置定时器类型为精确定时器
	QObject::connect(start_button, &QPushButton::clicked, this, &Chronograph::start_button_clicked); // 开始按钮点击事件
	QObject::connect(reset_record_button,&QPushButton::clicked, this, &Chronograph::reset); // 重置按钮点击事件
	QObject::connect(save_button, &QPushButton::clicked, this, &Chronograph::save_time);
	QObject::connect(timer, &QTimer::timeout, this, &Chronograph::update_display); // 计时器事件
}
    

Chronograph::~Chronograph() {
    
}

void Chronograph::start_button_clicked(){
    if(is_running){ // 停止计时
		stop();
		is_running = false;
		reset_record_button->setEnabled(true);
		save_button->setEnabled(true);
	}else{ // 开始计时
		start();
		is_running = true;
		reset_record_button->setEnabled(false);
		save_button->setEnabled(false);
    }
}

void Chronograph::reset(){
	time.reset(); // 计时类置零
	// 刷新显示
	hour_lcd->display((int)time.get_hours());
	minute_lcd->display((int)time.get_minutes());
	second_lcd->display((int)time.get_seconds());
}

void Chronograph::start() {
    start_button->setText("暂停");
    timer->start();
}

void Chronograph::stop(){
    start_button->setText("开始");
	timer->stop();
}

void Chronograph::update_display(){ 
	time.add_one_second(); // 时间加一秒
    hour_lcd->display((int)time.get_hours());
    minute_lcd->display((int)time.get_minutes());
    second_lcd->display((int)time.get_seconds());
}

void Chronograph::save_time()
{
	Timer::save_chronograph_time(time.get_hours(), time.get_minutes(), time.get_seconds());
}

} // namespace Timer
